package com.mobilepay.moneypig.model.application;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.annotation.Nullable;

import com.meituan.robust.Patch;
import com.meituan.robust.RobustCallBack;
import com.mobilepay.moneypig.config.Constant;
import com.mobilepay.moneypig.utils.LogDebug;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

import java.io.File;
import java.util.List;

/**
 * Created by xlm on 2017/8/17 0017
 */

public class AppIntentService extends IntentService {
    private static final String TAG = "AppIntentService";
    private static final String ACTION_INIT_WHEN_APP_CREATE = "com.mobilepay.moneypig.service.action.INIT";

    /**
     * Creates an IntentService.  Invoked by your subclass's constructor.
     */
    public AppIntentService() {
        super("AppIntentService");
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AppIntentService.class);
        intent.setAction(ACTION_INIT_WHEN_APP_CREATE);
        context.startService(intent);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_INIT_WHEN_APP_CREATE.equals(action)) {
                performInit();
//                getDir();//创建文件夹，存放path.jar
//                new PatchExecutor(getApplicationContext(), new PatchManipulateImp(), new HotFixcallBack()).start();
            }
        }
    }

    private void performInit() {
        Config.DEBUG = false;
        UMShareAPI.get(this.getApplicationContext());
        PlatformConfig.setWeixin(Constant.WEIXIN_APP_ID, Constant.WEIXIN_APP_KEY);
        PlatformConfig.setSinaWeibo(Constant.WB_APP_ID, Constant.WB_APP_KEY, "http://sns.whalecloud.com");
        PlatformConfig.setQQZone(Constant.QQ_APP_ID, Constant.QQ_APP_KEY);
        PlatformConfig.setAlipay(Constant.ALIPAY_APPID);
        //各个平台的配置，建议放在全局Application或者程序入口
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }

    private class HotFixcallBack implements RobustCallBack {
        @Override
        public void onPatchListFetched(boolean result, boolean isNet, List<Patch> patches) {
            LogDebug.i(TAG, "result>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + result);
        }

        @Override
        public void onPatchFetched(boolean result, boolean isNet, Patch patch) {
        }

        @Override
        public void onPatchApplied(boolean result, Patch patch) {
        }

        @Override
        public void logNotify(String log, String where) {
        }

        @Override
        public void exceptionNotify(Throwable throwable, String where) {
        }
    }

    int count = 0;

    private File getDir() {
        StringBuilder path = new StringBuilder();
        if (isSDAvailable()) {
            path.append(Environment.getExternalStorageDirectory()
                    .getPath());
            path.append(File.separator);// '/'
            path.append("HotFix");// /mnt/sdcard/HotFix
        } else {
            //如果SD卡不可用就在内存创建
            File filesDir = getApplication().getCacheDir();//cache  getFileDir file
            path.append(filesDir.getAbsolutePath());
        }
        File file = new File(path.toString());
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();// 创建文件夹
            count += 10;
        }
        return file;
    }

    private boolean isSDAvailable() {
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }
}
